import os
import cv2
from aip import AipBodyAnalysis
from aip import AipSpeech
from threading import Thread
import time


""" 你的 APPID AK SK """
APP_ID = '26386723'
API_KEY = 'uu6Hnn4p9udcKZgHRpmbQRRS'
SECRET_KEY = 'Km3DloZPV2hy9uzULQ8VqBDUMIsMphVW'
''' 调用'''

hand = {'One': '数字1', 'Five': '数字5', 'Fist': '拳头', 'Ok': 'OK',
        'Prayer': '祈祷', 'Congratulation': '作揖', 'Honour': '作别',
        'Heart_single': '比心心', 'Thumb_up': '点赞', 'Thumb_down': 'Diss',
        'ILY': '我爱你', 'Palm_up': '掌心向上', 'Heart_1': '双手比心1',
        'Heart_2': '双手比心2', 'Heart_3': '双手比心3', 'Two': '数字2',
        'Three': '数字3', 'Four': '数字4', 'Six': '数字6', 'Seven': '数字7',
        'Eight': '数字8', 'Nine': '数字9', 'Rock': 'Rock', 'Insult': '竖中指', 'Face': '脸'}

# 手势识别
gesture_client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)

capture = cv2.VideoCapture(0)  # 0为默认摄像头


def camera():
    while True:
        # 获得图片
        ret, frame = capture.read()
        # cv2.imshow(窗口名称, 窗口显示的图像)
        # 显示图片
        cv2.imshow('frame', frame)
        if cv2.waitKey(1) == ord('q'):
            break


Thread(target=camera).start()  # 引入线程防止在识别的时候卡死


def gesture_recognition():
    # 第一个参数ret 为True 或者False,代表有没有读取到图片

    # 第二个参数frame表示截取到一帧的图片

    while True:
        try:
            ret, frame = capture.read()

            # 图片格式转换
            image = cv2.imencode('.jpg', frame)[1]

            gesture = gesture_client.gesture(image)  # AipBodyAnalysis内部函数
            words = gesture['result'][0]['classname']


            print(hand[words])

        except:
            if cv2.waitKey(1) == ord('q'):
                break



#gesture_recognition()
